﻿// Xshell Command - Timeout
// 
// <copyright file="TimeoutCommand.cs" company="TaskyMedia LLC">
//   Copyright © 2010 TaskyMedia LLC
// </copyright>

namespace Xshell.Commands
{
    using System;
    using System.Threading;

    /// <summary>
    /// Pauses execution of a TaskSet for the specified interval.
    /// </summary>
    [XsCommand("Timeout", typeof(TimeoutCommandArgs))]
    internal class TimeoutCommand : IXsCommand
    {
        /// <summary>
        /// Executes the command with the specified arguments.
        /// </summary>
        /// <param name="args">The command arguments.</param>
        public void ExecuteCommand(IXsCommandArgs args)
        {
            // Validate the arguments
            if (args == null)
            {
                throw new InvalidOperationException("Command arguments are required for this operation");
            }

            if (!(args is SetRegistrySzCommandArgs))
            {
                throw new InvalidOperationException("Invalid command argument container type");
            }

            TimeoutCommandArgs timeoutArgs = (TimeoutCommandArgs)args;
            if (String.IsNullOrEmpty(timeoutArgs.Length))
            {
                throw new InvalidOperationException("Length is a required argument");
            }

            int timeoutLength = 0;
            if (!Int32.TryParse(timeoutArgs.Length, out timeoutLength))
            {
                throw new InvalidOperationException("Length not a valid number");
            }

            Thread.Sleep(timeoutLength);
        }
    }
}
